/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs.wizards;

import org.eclipse.swt.widgets.Text;

import org.digitalsoul.switchplugin.prefs.util.HelpTexts;
import org.digitalsoul.switchplugin.prefs.util.ItemListConfigurator;
import org.digitalsoul.switchplugin.prefs.util.UITools;

import org.digitalsoul.switchplugin.prefs.CriteriaListUI;

import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

abstract class CriteriaWizard extends Wizard {

    protected final ItemListConfigurator configurator;
    private final Page page;
    protected final CriteriaListUI listUI;
    protected Combo combo;
    protected Composite panel;
    protected Composite mainPanel;
    protected Button checkbox;
    private Label helpLabel;
    protected Text text;

    CriteriaWizard(ItemListConfigurator configurator, CriteriaListUI listUI) {
        this.configurator = configurator;
        this.listUI = listUI;
        page = new Page(this, getPageId());
        addPage(page);
    }

    @Override
    public String getWindowTitle() {
        return getTitle();
    }

    @Override
    public boolean canFinish() {
        return isWizardComplete();
    }

    @Override
    public boolean needsPreviousAndNextButtons() {
        return false;
    }

    public void addInverseCheckbox() {
        checkbox = new Button(panel, SWT.CHECK);
        checkbox.setText(getCheckboxText());
        GridData data = new GridData();
        data.verticalAlignment = GridData.END;
        checkbox.setLayoutData(data);
    }

    private final class Page extends WizardPage {
        protected Page(CriteriaWizard criteriaWizard, String pageId) {
            super(pageId);
        }

        @Override
        public String getDescription() {
            return getPageDescription();
        }

        public void createControl(Composite parent) {
            
            // MAIN PANEL
            mainPanel = new Composite(parent, SWT.NONE);
            GridLayout gridLayout = new GridLayout();
            gridLayout.marginTop = 12;
            gridLayout.marginLeft = 12;
            
            mainPanel.setLayout(gridLayout);
            setControl(mainPanel);

            // COMBO
            combo = new Combo(mainPanel, SWT.READ_ONLY);
            combo.setItems(getComboItems());
            combo.select(0);
            combo.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event arg0) {
                    helpLabel.setText(getHelpText());
                    helpLabel.update();
                    updateBuilderPanel();
                    updateButtons();
                    text.forceFocus();
                }
            });

            // PANEL
            panel = UITools.createGridPanel(mainPanel);
            panel.setLayout(new GridLayout());
            panel.setLayoutData(new GridData(GridData.FILL_BOTH));

            // HELP LABEL
            helpLabel = UITools.addHelpLabel(panel, getHelpText());
            helpLabel.setLayoutData(new GridData(GridData.FILL_BOTH));
            updateBuilderPanel();
        }
    }

    protected void updatePanels() {
        panel.layout();
        panel.update();
        mainPanel.layout();
        mainPanel.update();
    }

    protected void updateButtons() {
        getContainer().updateButtons();
    }

    protected void updateBuilderPanel() {
        for (Control c : panel.getChildren()) {
            if (c != helpLabel) {
                c.dispose();
            }
        }
        createBuilderPanelContent();
        updatePanels();
    }

    protected String getHelpText() {
        String itemName = combo.getItem(combo.getSelectionIndex());
        return HelpTexts.getHelpText(getTypeForItemName(itemName));
    }
    
    protected String getExampleText() {
        String itemName = combo.getItem(combo.getSelectionIndex());
        return HelpTexts.getExampleText(getTypeForItemName(itemName));
    }
    
    protected Composite createTextFieldPanel(String labelText) {
        // TEXTFIELD PANEL
        Composite textFieldPanel = UITools.createGridPanel(panel, 2);
        GridData gridData = new GridData(GridData.FILL_BOTH);
        gridData.verticalAlignment = GridData.CENTER;
        textFieldPanel.setLayoutData(gridData);

        UITools.addLabel(textFieldPanel, labelText);
        text = UITools.addTextField(textFieldPanel);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        text.addListener(SWT.Modify, new Listener() {
            public void handleEvent(Event arg0) {
                updateButtons();
            }
        });
        return textFieldPanel;
    }
    
    protected void addHelpLabel(Composite panel, String text) {
        UITools.addLabel(panel, "");
        UITools.addHelpLabel(panel, text);
    }

    protected abstract void createBuilderPanelContent();
    protected abstract Enum<?> getTypeForItemName(String itemName);
    protected abstract String getPageDescription();
    protected abstract String getTitle();
    protected abstract String getPageId();
    protected abstract String getCheckboxText();
    protected abstract boolean isWizardComplete();
    public abstract String[] getComboItems();
}
